package com.cheyun.netsalev3.viewmodel

import android.content.Intent
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.MutableLiveData
import com.cheyun.netsalev3.bean.*
import com.cheyun.netsalev3.bean.dms.Product
import com.cheyun.netsalev3.repository.ChooseCarRepository
import com.cheyun.netsalev3.utils.LettersComparator
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.view.mars.choosecar.MarsChooseCarActivity
import java.io.Serializable
import java.util.*
import kotlin.collections.ArrayList

class MarsChooseCarViewModel(var chooseCarRepository: ChooseCarRepository, var carBrandParam: CarBrandParam,var drive:Boolean,var stime:String,var enableClickAll:Boolean) : BaseViewModel() {
    //只选择品牌
    private var Isbrand = false
    //只选择到车系
    private var Isseries = false
    //只选择到车型
    private var Isspec = false
    //
    private var level = 0 //0 resultcode:102, 1 resultcode:104 , 2 resultcode:105



    //车系
    var cexiList = MutableLiveData<List<Product>>()
    //车型
    var carXingList = MutableLiveData<List<Product>>()
    //产品
    var productList = MutableLiveData<List<Product>>()

    init {
        if(drive){
            chooseCarRepository.getDriveseries(stime,"1","",  this::setSeries)
        }else {
            chooseCarRepository.getDMSseries("1", "", this::setChex)
        }
    }


    // 点击品牌
    fun onClickBrand(view: View, item: AZItemEntity<CarPinPaParam>) {
        item.value?.apply {
            carBrandParam.brand = this.id.toString()
            carBrandParam.brandname = this.title
        }

        //车系车型清空
        carBrandParam.series = ""
        carBrandParam.seriesname = ""
        carBrandParam.spec = ""
        carBrandParam.specname = ""

    }

    // 点击车系
    fun onClickCeXi(view: View, item: Product) {
        if(item.is_retail == 0 && !enableClickAll)return
        carBrandParam.series = item.id.toString()
        carBrandParam.seriesname = item.title
        if(item.spec_select != null && item.spec_select.isNotEmpty()){
            carBrandParam.spec_select = item.spec_select.toInt()
        }

        var tem = cexiList.value
        tem?.apply {
            for (i in this) {
                i.IsSelect = item.id == i.id
            }
        }
        cexiList.postValue(tem)
        //车型清空
        carBrandParam.spec = ""
        carBrandParam.specname = ""

        if (Isseries) {
            var context = view.context
            if (context is AppCompatActivity) {
                var intent = Intent()
                intent.putExtra("car", carBrandParam as Serializable)
                var resultCode = 102
                if(level == 1) resultCode = 104
                else if(level == 2)resultCode = 105
                context.setResult(resultCode, intent)
                context.finish()
            }
        } else {
            //获取车型
            item?.id?.apply {
                if(drive){
                    chooseCarRepository.getDriveseries(stime,"2", this.toString(), this@MarsChooseCarViewModel::setDriveSpec)
                }else{
                    chooseCarRepository.getDMSseries("2", this.toString(), this@MarsChooseCarViewModel::setChex)
                }

            }
            var actvity = view.context as MarsChooseCarActivity
            actvity.binding.viewPager.currentItem = 1

        }
    }

    // 点击车型
    fun onClickCar(view: View, item: Product) {
        if(item.is_retail == 0 && !enableClickAll)return
        carBrandParam.spec = item.id.toString()
        carBrandParam.specname = item.title
        var tem = carXingList.value
        tem?.apply {
            for (i in this) {
                i.IsSelect = item.id == i.id
            }
        }
        carXingList.postValue(tem)
        if(Isspec){
            var context = view.context
            if (context is AppCompatActivity) {
                var intent = Intent()
                intent.putExtra("car", carBrandParam as Serializable)
                context.setResult(102, intent)
                context.finish()
            }
        }else{
            //获取产品
            item?.id?.apply {
                chooseCarRepository.getDMSseries("3", this.toString(), this@MarsChooseCarViewModel::setChex)
            }
            var actvity = view.context as MarsChooseCarActivity
            actvity.binding.viewPager.currentItem = 2
        }
    }

    // 点击产品
    fun onClickProduct(view: View, item: Product) {
        if(item.is_retail == 0 && !enableClickAll)return
        carBrandParam.aic = item.id.toString()
        carBrandParam.aicname = item.title
        carBrandParam.excolor_code = item.excolor_code
        carBrandParam.intercolor_s_code = item.intercolor_s_code
        //carBrandParam.
        var tem = productList.value
        tem?.apply {
            for (i in this) {
                i.IsSelect = item.id == i.id
            }
        }
        productList.postValue(tem)
        var context = view.context
        if (context is AppCompatActivity) {
            var intent = Intent()
            intent.putExtra("car", carBrandParam as Serializable)
            context.setResult(102, intent)
            context.finish()
        }
    }

    /**
     *   获取车型车系回调
     */
    fun setChex(type:String,list: List<Product>) {
        when (type) {
            "1" -> cexiList.postValue(list)
            "2" -> carXingList.postValue(list)
            "3" -> productList.postValue(list)
        }
    }

    fun setSeries(list: List<Product>) {
        list.forEach { it.is_retail = 1 }
        cexiList.postValue(list)
    }

    fun setDriveSpec(list: List<Product>) {
        list.forEach { it.is_retail = 1 }
        carXingList.postValue(list)
    }

    fun initData(i: Int) {
        when (i) {
            1 -> {
                chooseCarRepository.getDMSseries("1","", this@MarsChooseCarViewModel::setChex)
            }
            2 -> {
                chooseCarRepository.getDMSseries("2", carBrandParam.series,this@MarsChooseCarViewModel::setChex)
            }
            3 -> {
                chooseCarRepository.getDMSseries("3", carBrandParam.spec,this@MarsChooseCarViewModel::setChex)
            }
        }
    }

    /**
     * 只选择品牌
     */
    fun setIsbrand(Isbrand: Boolean) {
        this.Isbrand = Isbrand
    }

    /**
     * 只选择到车系
     */
    fun setIsseries(booleanExtra: Boolean) {
        Isseries = booleanExtra
    }

    /**
     * 只选择到车型
     */
    fun setIsspec(booleanExtra: Boolean) {
        Isspec = booleanExtra
    }

    fun setLevel(v: Int) {
        level = v
    }
}
